<?php
/**
 * Created by Silent Night.
 * User: Silent Night
 * Date: 2017/9/26
 * Time: 11:13
 */

namespace app\api\controller;

use think\cache\driver\Redis;
use think\Controller;

use app\api\model;
use think\Exception;
use think\Hook;
use think\Route;
use \think\response\Json;


class Payment extends Controller
{

    public function lang(){

    }
    public function eee()
    {
        echo "ddddd";
        $config = config("cacheKey");
        print_r($config);
    }

    public function aaa()
    {

        $userInfo = array('id' => 11);
        $dataid = "98";
        $result = recordFiles($userInfo, $dataid);
        print_r($result);
        $result = json_decode($result, true);

        return json($result);
    }

    public function bbb()
    {

        $userInfo = array('id' => 10);
        $repid = "95";
        $data = loadSummary($userInfo, $repid);
        $result = analysisSummaryData($data, $userInfo['id'], $repid);
        $result['data'] = json_decode($data, true);
        return json($result);
    }


    public function ccc()
    {
        $userInfo = array('id' => 10);
        $repid = "95";
        $data = loadCurves($userInfo, $repid);

        $result = analysisCurvesData($data, $userInfo['id'], $repid);
        $result['data'] = json_decode($data, true);
        return json($result);
    }

//    public $alipay_url = "http://api.dongshuimian.com/api/order/alipay_n";
    public $alipay_url = "http://api.dongshuimian.com/api/order/alipay_n";


    public function addShopOrder()
    {

    }

    /**
     * @return Json 支付宝商品下单接口
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function alipayGoods()
    {
        $time = date("Y-m-d H:i:s");
        $auth = trim(input("request.auth"));//auth

        $coupon_id = trim(input("request.coupon_id"));//暂时不用 优惠券ID
        $coupon_money = trim(input("request.coupon_money"));//暂时不用 优惠券金额
        $user_memo = trim(input("request.user_memo"));//用户留言
        $user_addrInfo = trim(input("request.user_addrInfo"));//用户收货信息{"t_name":"收货人姓名","t_phone":"收货人联系电话","t_provice":"省","t_city":"市","t_area":"区","t_address":"详细地址","t_zipcode":"邮编"}
        $goods = trim(input("request.goods"));//商品 [{id:19,num:1,good_addr:'红色,绿色'},{id:18,num:1,good_addr:'红色,绿色'}]

        if (empty($user_memo)) {
            $user_memo = "";
        }
        if (empty($coupon_id)) {
            $coupon_id = 0;
        }
        if (empty($coupon_money)) {
            $coupon_money = 0;
        }
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        if (empty($user_addrInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,请填写用户收货信息', 'devMsg' => '操作失败，参数错误[user_addrInfo]'];
            return json($return);
        }

        $user_addrInfo = json_decode($user_addrInfo, true);
        if (empty($user_addrInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,请填写用户收货信息', 'devMsg' => '操作失败，参数错误[user_addrInfo]'];
            return json($return);
        }

        if (empty($user_addrInfo['t_name'])) {
            $return = ['err' => 1, 'msg' => '操作失败,请填写收货人姓名', 'devMsg' => '操作失败，参数错误[user_addrInfo]'];
            return json($return);
        }
        if (empty($user_addrInfo['t_phone'])) {
            $return = ['err' => 1, 'msg' => '操作失败,请填写联系电话', 'devMsg' => '操作失败，参数错误[user_addrInfo]'];
            return json($return);
        }
        if (empty($user_addrInfo['t_address'])) {
            $return = ['err' => 1, 'msg' => '操作失败,请填写收货详细地址', 'devMsg' => '操作失败，参数错误[user_addrInfo]'];
            return json($return);
        }
//        if (empty($user_addrInfo['t_zipcode'])) {
//            $return = ['err' => 1, 'msg' => '操作失败,请填写邮编', 'devMsg' => '操作失败，参数错误[user_addrInfo]'];
//            return json($return);
//        }
        if (empty($goods)) {
            $return = ['err' => 1, 'msg' => '操作失败,请选择商品', 'devMsg' => '操作失败，参数错误[goods]'];
            return json($return);
        }

        $goods = json_decode($goods, true);

        if (empty($goods)) {
            $return = ['err' => 2, 'msg' => '操作失败,请选择商品', 'devMsg' => '操作失败，参数错误[goods]'];
            return json($return);
        }
        $carList = array();
        $totleMoney = 0;//总的订单金额
        foreach ($goods as $key => $val) {
            if (empty($val['id'])) {
                $return = ['err' => 1, 'msg' => '操作失败,商品不存在', 'devMsg' => '操作失败，参数错误[goods]'];
                return json($return);
            }
            $goodInfo = getGoodInfo($val['id']);
            if (empty($goodInfo)) {
                $return = ['err' => 1, 'msg' => '操作失败,商品不存在', 'devMsg' => '操作失败，参数错误[goods]'];
                return json($return);
            }
            if ($goodInfo['is_delete'] == 1) {
                $return = ['err' => 1, 'msg' => '操作失败,[' . $goodInfo['name'] . "]已下架", 'devMsg' => '操作失败,[' . $goodInfo['name'] . "]已下架"];
                return json($return);
            }
            if ($goodInfo['stats'] == 1) {
                $return = ['err' => 1, 'msg' => '操作失败,[' . $goodInfo['name'] . "]已下架", 'devMsg' => '操作失败,[' . $goodInfo['name'] . "]已下架"];
                return json($return);
            }
            if (empty($val['num'])) {
                $return = ['err' => 1, 'msg' => '操作失败,[' . $goodInfo['name'] . "]的购买数量不正确", 'devMsg' => '操作失败,[' . $goodInfo['name'] . "]的购买数量不正确"];
                return json($return);
            }
            if ($val['num'] <= 0) {
                $return = ['err' => 1, 'msg' => '操作失败,[' . $goodInfo['name'] . "]的购买数量不正确", 'devMsg' => '操作失败,[' . $goodInfo['name'] . "]的购买数量不正确"];
                return json($return);
            }
            if ($goodInfo['number'] < $val['num']) {
                $return = ['err' => 1, 'msg' => '操作失败,[' . $goodInfo['name'] . "]库存不足", 'devMsg' => '操作失败,[' . $goodInfo['name'] . "]库存不足"];
                return json($return);
            }
            $goodInfo['buy_num'] = $val['num'];
            $goodInfo['buy_good_addr'] = $val['good_addr'];
            $totleMoney += $goodInfo['buy_num'] * $goodInfo['price'];
            $carList[] = $goodInfo;
        }
        if (empty($carList)) {
            $return = ['err' => 1, 'msg' => '操作失败,请选择商品', 'devMsg' => '操作失败，参数错误[goods]'];
            return json($return);
        }
        $res = addGoodOrder($userInfo, $carList, $user_addrInfo, $user_memo, $totleMoney, $coupon_id, $coupon_money);
        if ($res['err'] == 0) {
            $order_sn = $res['data']['order_sn'];
            $order_money = $res['data']['money'];

            $string = alipay_pay($order_sn, $order_money, $this->alipay_url,"购买商品");
            $return['err'] = 0;
            $return['msg'] = '成功';
            $this->addRunTask($order_sn, 60);//1个小时内未支付，将关闭订单
            $return['data'] = base64_encode($string);
            $return['order_sn'] = $order_sn;
            addOrderTrackLog(array(
                'ctime'=>date("Y-m-d H:i:s"),
                'order_sn'=>$order_sn,
                'op_uid'=>$userInfo['id'],
                'op_username'=>$userInfo['uname']."[".$userInfo['nickname']."]",
                'act'=>"下单",
            ));
            addUserAddress($userInfo['id'],$user_addrInfo);
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '订单生成失败';
            return json($return);
        }

    }


    /**
     * @return Json 再次调用支付宝支付
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAlipayGoodsTicket()
    {
        $auth = trim(input("request.auth"));//用户auth
        $order_sn = trim(input("request.order_sn"));//帖子ID
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        if (empty($order_sn)) {
            $return = ['err' => 2, 'msg' => '操作失败,订单号为空', 'devMsg' => '操作失败，参数错误[order_sn]'];
            return json($return);
        }
        $payOrderMode = new \app\admin\model\PayOrder();
        $orderInfo = $payOrderMode->getDataForWhere(array(
            'order_sn' => $order_sn
        ));
        if (empty($orderInfo))//订单号不存在
        {
            $return = ['err' => 3, 'msg' => '操作失败,订单不存在', 'devMsg' => '操作失败，参数错误[order_sn]'];
            return json($return);
        }

        if ($orderInfo['uid'] != $userInfo['id']) {
            $return = ['err' => 4, 'msg' => '操作失败，对不起，您无权操作该订单', 'devMsg' => '操作失败'];
            return json($return);
        }

        if ($orderInfo['is_delete'] == 1) {
            $return = ['err' => 5, 'msg' => '获取失败，对不起，该订单已经不存在', 'devMsg' => '获取失败'];
            return json($return);
        }


        if ($orderInfo['status'] >0 && $orderInfo['status']<4) {
            $return = ['err' => 6, 'msg' => '该订单已经付款', 'devMsg' => "该订单已经付款"];
            return json($return);
        }

        if ($orderInfo['status']==4) {
            $return = ['err' => 6, 'msg' => '订单已经被取消', 'devMsg' => "订单已经被取消"];
            return json($return);
        }


            $order_sn = $orderInfo['order_sn'];
            $order_money = $orderInfo['money'];
            $string = alipay_pay($order_sn, $order_money, $this->alipay_url,"购买商品");
            $return['err'] = 0;
            $return['msg'] = '成功';
            $return['data'] = base64_encode($string);
            $return['order_sn'] = $order_sn;
            addOrderTrackLog(array(
                'ctime'=>date("Y-m-d H:i:s"),
                'order_sn'=>$order_sn,
                'op_uid'=>$userInfo['id'],
                'op_username'=>$userInfo['uname']."[".$userInfo['nickname']."]",
                'act'=>"下单",
            ));
            return json($return);
    }

    /**
     * 支付宝支付函数
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function alipay()
    {
        $time = date("Y-m-d H:i:s");
        $auth = trim(input("request.auth"));//auth
        $classid = trim(input("request.classid"));//挑战ID
        $num = trim(input("request.num"));//购买数量
        $coupon_id = trim(input("request.coupon_id"));//暂时不用 优惠券ID
        $coupon_money = trim(input("request.coupon_money"));//暂时不用 优惠券金额

        if (empty($coupon_id)) {
            $coupon_id = 0;
        }
        if (empty($coupon_money)) {
            $coupon_money = 0;
        }
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        if (empty($num)) {
            $return['err'] = 11001;
            $return['msg'] = '购买数量不能够为空';
            return json($return);
        }


        if (empty($classid)) {
            $return['err'] = 11001;
            $return['msg'] = '挑战ID错误';
            return json($return);
        }

        if (isClasslistPay($userInfo['id'], $classid) == 1) {
            $return['err'] = 11001;
            $return['msg'] = "该挑战您已经加入，无需重复加入";
            return json($return);
        }
        $classListMode = new \app\admin\model\Classlist();
        $classInfo = $classListMode->getDataForWhere(array('id' => $classid));
        if (empty($classInfo)) {
            $return['err'] = 11001;
            $return['msg'] = '挑战ID错误';
            return json($return);
        }
        if ($classInfo['money'] <= 0) {
            $return['err'] = 11001;
            $return['msg'] = '挑战免费无需支付';
            return json($return);
        }


        if ($num > $classInfo['max']) {
            $return['err'] = 11001;
            $return['msg'] = '购买数量已经超过最大值';
            return json($return);
        }
        $money = $classInfo['money'];

        $order_sn = build_order_no();
        $money = rounding($num * $classInfo['money'] - $coupon_money, 2);

        //添加订单
        $orderData = array(
            'order_sn' => $order_sn,
            'uid' => $userInfo['id'],
            'ctime' => $time,
            'stat' => 0,
            'money' => $money,
            'classid' => $classid,
            'num' => $num,
            'price' => $classInfo['money'],
            'coupon_id' => $coupon_id,
            'coupon_money' => $coupon_money,
            'pay_type' => 2,
            'ty' => 0//0是课程 1是商品
        );
        $payOrderMode = new \app\admin\model\PayOrder();
        $res = $payOrderMode->add($orderData);
        if ($res) {
            $order_sn = $orderData['order_sn'];
            $string = alipay_pay($order_sn, $orderData['money'], $this->alipay_url);
            $this->addRunTask($order_sn, 60);//1个小时内未支付将关闭订单
            $return['err'] = 0;
            $return['msg'] = '成功';

            $return['data'] = base64_encode($string);
            $return['order_sn'] = $order_sn;
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '订单生成失败';
            return json($return);
        }
    }


    /**
     * 钱包支付
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function walletpay()
    {
        $time = date("Y-m-d H:i:s");
        $auth = trim(input("request.auth"));//auth
        $classid = trim(input("request.classid"));//挑战ID
        $num = trim(input("request.num"));//购买数量
        $coupon_id = trim(input("request.coupon_id"));//暂时不用 优惠券ID
        $coupon_money = trim(input("request.coupon_money"));//暂时不用 优惠券金额
        if (empty($ty)) {
            $ty = 0;
        } else {
            $ty = 1;
        }
        if (empty($coupon_id)) {
            $coupon_id = 0;
        }
        if (empty($coupon_money)) {
            $coupon_money = 0;
        }
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        if (empty($num)) {
            $return['err'] = 11001;
            $return['msg'] = '购买数量不能够为空';
            return json($return);
        }


        if (empty($classid)) {
            $return['err'] = 11001;
            $return['msg'] = '挑战ID错误';
            return json($return);
        }


        if (isClasslistPay($userInfo['id'], $classid) == 1) {
            $return['err'] = 11001;
            $return['msg'] = "该挑战您已经加入，无需重复加入";
            return json($return);
        }
        $classListMode = new \app\admin\model\Classlist();
        $classInfo = $classListMode->getDataForWhere(array('id' => $classid));
        if (empty($classInfo)) {
            $return['err'] = 11001;
            $return['msg'] = '挑战ID错误';
            return json($return);
        }
        if ($classInfo['money'] <= 0) {
            $return['err'] = 11001;
            $return['msg'] = '挑战免费无需支付';
            return json($return);
        }


        if ($num > $classInfo['max']) {
            $return['err'] = 11001;
            $return['msg'] = '购买数量已经超过最大值';
            return json($return);
        }
        $result = walletpay($userInfo['id'], $classInfo, $num, $coupon_id, $coupon_money);
        if ($result['err'] == 0) {

            $data = array(
                'uid' => $userInfo['id'],
                'classid' => $classInfo['id'],
                'payno' => $result['data']['order_sn'],
                'ctime' => date('Y-m-d H:i:s'),
                'stat' => 0,
                'money' => $result['data']['money'],
                'price' => $result['data']['price'],
                'num' => $result['data']['num'],
            );
            joinInClass($data);//加入课程
            addPlanCheckIn($userInfo['id'], $classInfo);//加入打卡计划
            //写入金额明细,无金额变动，无需写入金额变动明细
            addClasslistTotleMoneyAndNum($classInfo['id'], $result['data']['money'], $result['data']['num']);
        }
        return json($result);
    }

    public function walletpayGoods()
    {
        $time = date("Y-m-d H:i:s");
        $auth = trim(input("request.auth"));//auth

        $coupon_id = trim(input("request.coupon_id"));//暂时不用 优惠券ID
        $coupon_money = trim(input("request.coupon_money"));//暂时不用 优惠券金额
        $user_memo = trim(input("request.user_memo"));//用户留言
        $user_addrInfo = trim(input("request.user_addrInfo"));//用户收货信息{"t_name":"收货人姓名","t_phone":"收货人联系电话","t_provice":"省","t_city":"市","t_area":"区","t_address":"详细地址","t_zipcode":"邮编"}
        $goods = trim(input("request.goods"));//商品 [{id:19,num:1,good_addr:'红色,绿色'},{id:18,num:1,good_addr:'红色,绿色'}]

        if (empty($user_memo)) {
            $user_memo = "";
        }
        if (empty($coupon_id)) {
            $coupon_id = 0;
        }
        if (empty($coupon_money)) {
            $coupon_money = 0;
        }
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        if (empty($user_addrInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,请填写用户收货信息', 'devMsg' => '操作失败，参数错误[user_addrInfo]'];
            return json($return);
        }

        $user_addrInfo = json_decode($user_addrInfo, true);
        if (empty($user_addrInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,请填写用户收货信息', 'devMsg' => '操作失败，参数错误[user_addrInfo]'];
            return json($return);
        }

        if (empty($user_addrInfo['t_name'])) {
            $return = ['err' => 1, 'msg' => '操作失败,请填写收货人姓名', 'devMsg' => '操作失败，参数错误[user_addrInfo]'];
            return json($return);
        }
        if (empty($user_addrInfo['t_phone'])) {
            $return = ['err' => 1, 'msg' => '操作失败,请填写联系电话', 'devMsg' => '操作失败，参数错误[user_addrInfo]'];
            return json($return);
        }
        if (empty($user_addrInfo['t_address'])) {
            $return = ['err' => 1, 'msg' => '操作失败,请填写收货详细地址', 'devMsg' => '操作失败，参数错误[user_addrInfo]'];
            return json($return);
        }
//        if (empty($user_addrInfo['t_zipcode'])) {
//            $return = ['err' => 1, 'msg' => '操作失败,请填写邮编', 'devMsg' => '操作失败，参数错误[user_addrInfo]'];
//            return json($return);
//        }
        if (empty($goods)) {
            $return = ['err' => 1, 'msg' => '操作失败,请选择商品', 'devMsg' => '操作失败，参数错误[goods]'];
            return json($return);
        }

        $goods = json_decode($goods, true);

        if (empty($goods)) {
            $return = ['err' => 2, 'msg' => '操作失败,请选择商品', 'devMsg' => '操作失败，参数错误[goods]'];
            return json($return);
        }
        $carList = array();
        $totleMoney = 0;//总的订单金额
        foreach ($goods as $key => $val) {
            if (empty($val['id'])) {
                $return = ['err' => 1, 'msg' => '操作失败,商品不存在', 'devMsg' => '操作失败，参数错误[goods]'];
                return json($return);
            }
            $goodInfo = getGoodInfo($val['id']);
            if (empty($goodInfo)) {
                $return = ['err' => 1, 'msg' => '操作失败,商品不存在', 'devMsg' => '操作失败，参数错误[goods]'];
                return json($return);
            }
            if ($goodInfo['is_delete'] == 1) {
                $return = ['err' => 1, 'msg' => '操作失败,[' . $goodInfo['name'] . "]已下架", 'devMsg' => '操作失败,[' . $goodInfo['name'] . "]已下架"];
                return json($return);
            }
            if ($goodInfo['stats'] == 1) {
                $return = ['err' => 1, 'msg' => '操作失败,[' . $goodInfo['name'] . "]已下架", 'devMsg' => '操作失败,[' . $goodInfo['name'] . "]已下架"];
                return json($return);
            }
            if (empty($val['num'])) {
                $return = ['err' => 1, 'msg' => '操作失败,[' . $goodInfo['name'] . "]的购买数量不正确", 'devMsg' => '操作失败,[' . $goodInfo['name'] . "]的购买数量不正确"];
                return json($return);
            }
            if ($val['num'] <= 0) {
                $return = ['err' => 1, 'msg' => '操作失败,[' . $goodInfo['name'] . "]的购买数量不正确", 'devMsg' => '操作失败,[' . $goodInfo['name'] . "]的购买数量不正确"];
                return json($return);
            }
            if ($goodInfo['number'] < $val['num']) {
                $return = ['err' => 1, 'msg' => '操作失败,[' . $goodInfo['name'] . "]库存不足", 'devMsg' => '操作失败,[' . $goodInfo['name'] . "]库存不足"];
                return json($return);
            }
            $goodInfo['buy_num'] = $val['num'];
            $goodInfo['buy_good_addr'] = $val['good_addr'];
            $totleMoney += $goodInfo['buy_num'] * $goodInfo['price'];
            $carList[] = $goodInfo;
        }
        if (empty($carList)) {
            $return = ['err' => 1, 'msg' => '操作失败,请选择商品', 'devMsg' => '操作失败，参数错误[goods]'];
            return json($return);
        }
        $res = addGoodOrderYuer($userInfo, $carList, $user_addrInfo, $user_memo, $totleMoney, $coupon_id, $coupon_money);
        if ($res['err'] == 0) {

            $return['err'] = 0;
            $return['msg'] = '成功';

            $return['data'] = $res['data'];
            addOrderTrackLog(array(
                'ctime'=>date("Y-m-d H:i:s"),
                'order_sn'=>$res['data']['order_sn'],
                'op_uid'=>$userInfo['id'],
                'op_username'=>$userInfo['uname']."[".$userInfo['nickname']."]",
                'act'=>"下单并支付",
            ));

            addUserAddress($userInfo['id'],$user_addrInfo);
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = $res['msg'];
            return json($return);
        }

    }


    /**
     * 关闭订单函数
     * @param $order_sn
     */
    public function closeOrderTask($order_sn)
    {
        $model = new \app\admin\model\PayOrder();
        $orderInfo = $model->getDataForWhere(array('order_sn' => $order_sn));
        if (empty($orderInfo)) {
            return 1000;
        }
        if ($orderInfo['stat'] == 1) {
            return 1002;
        }
        $res = $model->updateDataForWhere(array(
            'status' => 4,
            'stat'=>2
        ), array('id' => $orderInfo['id']));
        if ($res || $res == 0) {
            addOrderTrackLog(array(
                'ctime'=>date("Y-m-d H:i:s"),
                'order_sn'=>$orderInfo['order_sn'],
                'op_uid'=>0,
                'op_username'=>"",
                'act'=>"超时支付系统自动取消订单",
            ));
            return 0;
        } else {
            return 1003;
        }
    }

    /**
     * 添加订单任务
     * @param $order_sn
     * @param string $time
     */
    public function addRunTask($order_sn, $time = "0")
    {
        $model = new \app\admin\model\SystemTask();

        $systemTask = array(
            'task_name' => '订单30分钟未付款关闭订单任务',
            'task_desc' => '订单30分钟未付款关闭订单任务',
            'task_fun' => '\app\api\controller\Payment#closeOrderTask',
            'task_param' => $order_sn,
            'ctime' => date("Y-m-d H:i:s"),
            'stats' => '1',
            'task_type' => '0',
            'is_delete' => '0',
            'task_type' => '0',
        );
        if (!empty($time)) {
            $systemTask['stats'] = 0;
            $systemTask['task_type'] = 1;
            $systemTask['time_val'] = date("Y-m-d H:i:s", strtotime("+" . $time . "minutes"));
        }

        $insertID = $model->add($systemTask);
        if ($insertID) {

            $systemTask['id'] = $insertID;
            if (empty($time)) {//为0才立即执行
                addTaskQueue($systemTask);//添加app推送队列
            }

        }
    }


    /**
     * 测试关闭订单
     */
    public function testAddTask()
    {
        $this->addRunTask("order:" . time(), 2);
        $this->addRunTask("order:" . time() . "_1", 5);
        $this->addRunTask("order:" . time() . "_2", 6);
    }
}